package com.example.shopping_mall.fragment;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.shopping_mall.R;
import com.example.shopping_mall.adapter.RoderAdapter;
import com.example.shopping_mall.application.MineOrderPresenter;
import com.example.shopping_mall.bean.EndMineOrder;
import com.example.shopping_mall.bean.MineOrder;
import com.example.shopping_mall.bean.OrderItemList;
import com.example.shopping_mall.http.IMineOrderPresenter;

import java.util.ArrayList;
import java.util.List;


/**
 * 全部订单
 */
public class MineAllOrderFragment extends AllFragment {

    private static final String TAG = MineAllOrderFragment.class.getSimpleName();
    private RecyclerView allOrderRV;
    public IMineOrderPresenter mineOrderPresenter;

    //条目中列表
    private RecyclerView mineitemRV;
    List<Object> listO = new ArrayList<>();
    OrderItemList itemList;
    EndMineOrder endMineOrder;
    MineOrder order;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine_all_order;
    }

    @Override
    protected void initView() {
        mineOrderPresenter = new MineOrderPresenter(getContext());
        //初始化列表
        allOrderRV = findViewById(R.id.allOrderRV);
        allOrderRV.setLayoutManager(new LinearLayoutManager(getActivity()));

        initData();
    }

    private void initData() {

        mineOrderPresenter.requestOrder(1, new MineOrderPresenter.OnMineOrderListener() {
            @Override
            public void onMineOrder(MineOrder mineOrder) {

                List<List<OrderItemList>> orderList = new ArrayList<>();
                //System.out.println("订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单': " + mineOrder.getStatus());
                OrderItemList[] orderItemLists = mineOrder.getOrderItemList();
                List<OrderItemList> listitem = new ArrayList<>();
                for (int i = 0; i < orderItemLists.length; i++) {
                    itemList = new OrderItemList();
                    itemList.setProductPrice(orderItemLists[i].getProductPrice());
                    itemList.setProductPic(orderItemLists[i].getProductPic());
                    itemList.setProductName(orderItemLists[i].getProductName());
                    listitem.add(itemList);
                }
                orderList.add(listitem);

                //拆分数据
                if (orderList != null) {
                    for (int i = 0; i < orderList.size(); i++) {
                        order = new MineOrder();
                        order.setOrderSn(mineOrder.getOrderSn());
                        order.setTotalAmount(mineOrder.getTotalAmount());
                        order.setPayAmount(mineOrder.getPayAmount());
                        order.setStatus(mineOrder.getStatus());

                        endMineOrder = new EndMineOrder();
                        endMineOrder.setOrderSn(mineOrder.getOrderSn());
                        endMineOrder.setTotalAmount(mineOrder.getTotalAmount());
                        endMineOrder.setPayAmount(mineOrder.getPayAmount());
                        endMineOrder.setStatus(mineOrder.getStatus());


                        listO.add(order);
                        listO.addAll(orderList.get(i));
                        listO.add(endMineOrder);
                    }
                }
                RoderAdapter adapter = new RoderAdapter(listO, getActivity());
                allOrderRV.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        });
    }

}
